﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SqlHelperForCry.Common;
using Torch.WL.Entity;


namespace Torch.WL.Web.CleanChina
{
    public partial class reward : BasePage
    {

        private string imageUrl
        {
            get
            {
                if (ViewState["WebsiteImageUrl"] == null)
                    ViewState["WebsiteImageUrl"] = AppSettingsHelper.GetString("WebsiteImageUrl");
                string url = ViewState["WebsiteImageUrl"].ToString();
                return url;
            }
        }

        private DataTable dtSelect
        {
            get
            {

                return ViewState["dtSelect"] as DataTable;
            }
            set
            {
                ViewState["dtSelect"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            DataTable dt = DataAccess.Select(typeof(Tb_Reward), "1=1");
            dt.Columns.Add("companyNames", typeof(string));
            DataTable dtRGs = DataAccess.ExecuteSql("Torch.WL.ConnectionString", "select a.rewardId,b.* from Tb_RewardGrant a inner join Tb_Company b on a.companyId=b.companyId order by a.sortindex ").Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                DataRow[] drSelects = dtRGs.Select("rewardId='" + dr["rewardId"].ToString() + "'");
                string gsms = "";
                foreach (DataRow drRG in drSelects)
                {
                    if (string.IsNullOrEmpty(gsms))
                        gsms = drRG["companyName"].ToString();
                    else
                        gsms += "<br />" + drRG["companyName"].ToString();
                }
                dr["companyNames"] = gsms;
            }

            this.rptJzj.DataSource = dt;
            this.rptJzj.DataBind();
        }

        protected void rptJzj_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int rewardId = 0;
            int.TryParse(e.CommandArgument.ToString(), out rewardId);
            if (e.CommandName == "Delete")
            {
                Tb_Reward jzj = new Tb_Reward(rewardId);
                jzj.Delete();
                BindData();
                this.upJzjList.Update();
                WebCommon.ShowMessageForAjax(this, "已成功删除！", this.upJzjList);
            }
            if (e.CommandName == "Edit")
            {
                TextBox txt = e.Item.FindControl("txtRewardName") as TextBox;
                if (string.IsNullOrEmpty(txt.Text.Trim()))
                {
                    WebCommon.ShowMessageForAjax(this, "修改时，奖项名称不能为空!", this.upJzjList);
                    txt.Focus();
                    return;
                }
                Tb_Reward r = new Tb_Reward(rewardId);
                r.Load();
                r.rewardName = txt.Text.Trim();
                r.Save();

                BindData();
                this.upJzjList.Update();
                WebCommon.ShowMessageForAjax(this, "已成功修改!", this.upJzjList);
            }
            if (e.CommandName == "SetReward")
            {
                this.hfJzjId.Value = rewardId.ToString();
                dtSelect = null;
                this.txtGsmc.Text = "";
                BindHasSelectData();
                BindSearchData();
                this.upJzjDetail.Update();
                WebCommon.ShowDialog(this, "Jzj");
            }
            if (e.CommandName == "Add")
            {
                TextBox txt = e.Item.FindControl("txtRewardName") as TextBox;
                if (string.IsNullOrEmpty(txt.Text.Trim()))
                {
                    WebCommon.ShowMessageForAjax(this, "新增时，奖项名称不能为空!", this.upJzjList);
                    txt.Focus();
                    return;
                }
                Tb_Reward r = new Tb_Reward();
                r.rewardName = txt.Text.Trim();
                r.createTime = DateTime.Now;
                r.Save();

                BindData();
                this.upJzjList.Update();
                WebCommon.ShowMessageForAjax(this, "已成功新增!", this.upJzjList);
            }
        }

        protected void BindHasSelectData()
        {
            if (dtSelect == null)
            {
                DataSet ds = DataAccess.ExecuteSql("Torch.WL.ConnectionString", "select a.grantId,a.rewardId,a.sortindex,b.* from Tb_RewardGrant a inner join Tb_Company b on a.companyId=b.companyId where a.rewardId='" + this.hfJzjId.Value + "' order by a.sortindex");
                dtSelect = ds.Tables[0];
                dtSelect.Columns.Add("Gslogo", typeof(string));
                foreach (DataRow dr in dtSelect.Rows)
                    dr["Gslogo"] = "<img src='" + imageUrl + dr["companyLogo"] + "' alt='' />";
            }
            this.rptHasSelectZs.DataSource = dtSelect;
            this.rptHasSelectZs.DataBind();

            this.spHasZss.InnerText = this.dtSelect.Rows.Count.ToString();
            this.upJzjDetail.Update();
        }

        protected void lbtnSaveSelect_Click(object sender, EventArgs e)
        {
            int rewardId = 0;
            int.TryParse(this.hfJzjId.Value, out rewardId);
            foreach (DataRow dr in dtSelect.Select("grantId=-1"))
            {
                Tb_RewardGrant rg = new Tb_RewardGrant();
                rg.rewardId = rewardId;
                rg.companyId = int.Parse(dr["companyId"].ToString());
                rg.sortindex = dtSelect.Rows.Count;
                rg.Save();
            }

            string hasSelectComIds = "-1";
            foreach (DataRow dr in dtSelect.Select("1=1"))
                hasSelectComIds += "," + dr["companyId"];
            DataAccess.Delete(typeof(Tb_RewardGrant), "rewardId='" + rewardId + "' and companyId not in (" + hasSelectComIds + ")");

            BindData();
            this.upJzjList.Update();

            WebCommon.ShowMessageForAjax(this, "已成功保存!", this.upJzjList);
            WebCommon.CloseDialog(this, "Jzj_dialog");
        }

        protected void rptHasSelectZs_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            string[] args = e.CommandArgument.ToString().Split('|');
            int rewardId = 0;
            int companyId = 0;
            int.TryParse(args[0], out rewardId);
            int.TryParse(args[1], out companyId);
            if (e.CommandName == "Cancel")
            {
                DataRow[] drCancel = dtSelect.Select("rewardId='" + rewardId + "' and companyId='" + companyId + "'");
                if (drCancel != null && drCancel.Length > 0)
                    dtSelect.Rows.Remove(drCancel[0]);

                BindHasSelectData();
                BindSearchData();
                WebCommon.ShowMessageForAjax(this, "已成功取消选择！", this.upJzjDetail);
            }
        }

        protected void BindSearchData()
        {
            string hasSelectComIds = "-1";
            foreach (DataRow dr in dtSelect.Rows)
                hasSelectComIds += "," + dr["companyId"].ToString();

            DataSet ds = DataAccess.ExecuteSql("Torch.WL.ConnectionString", "select a.* from Tb_Company a where a.companyName like '%" + this.txtGsmc.Text.Trim().Replace("'", "") + "%' and a.companyId not in (" + hasSelectComIds + ") order by a.firstPingYinChar");
            DataTable dtSearchResult = ds.Tables[0];
            dtSearchResult.Columns.Add("Gslogo", typeof(string));
            foreach (DataRow dr in dtSearchResult.Rows)
                dr["Gslogo"] = "<img src='" + imageUrl + dr["companyLogo"] + "' alt='' />";

            this.rptUnSelectZs.DataSource = dtSearchResult;
            this.rptUnSelectZs.DataBind();
            this.upJzjDetail.Update();
        }

        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            BindSearchData();
        }


        protected void rptUnSelectZs_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            int rewardId = 0;
            int.TryParse(this.hfJzjId.Value, out rewardId);
            string[] args = e.CommandArgument.ToString().Split('|');

            int companyId = 0;
            int.TryParse(args[0], out companyId);
            string companyLogo = args[1], ZhanWeiH = args[2], companyName = args[3], companyTele = args[4];
            if (e.CommandName == "Select")
            {
                if (dtSelect != null && dtSelect.Rows.Count == 3)
                {
                    WebCommon.ShowMessageForAjax(this, "已无法选择公司，最多3家！", this.upJzjDetail);
                    return;
                }
                DataRow drNew = dtSelect.NewRow();
                drNew["grantId"] = -1;
                drNew["rewardId"] = rewardId;
                drNew["companyId"] = companyId;
                drNew["companyLogo"] = companyLogo;
                drNew["Gslogo"] = "<img src='" + imageUrl + companyLogo + "' alt='' />";
                drNew["ZhanWeiH"] = ZhanWeiH;
                drNew["companyName"] = companyName;
                drNew["companyTele"] = companyTele;
                dtSelect.Rows.Add(drNew);
                BindHasSelectData();
                BindSearchData();
            }
        }
    }
}